package com.xbongbong.formula.qlexpress.operator.advanced;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.Operator;
import com.xbongbong.formula.constant.Constant;
import com.xbongbong.formula.enums.MathEnum;
import com.xbongbong.paas.constant.PaasConstant;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 删除目标表单中符合条件的数据。目标表单可为系统中的任意表单（含子表），请谨慎执行此函数
 * @author 吴峰
 * @version v1.0
 * @date 2020/1/8 9:32
 * @since v1.0
 */
public class OperatorDelete extends Operator {

    public OperatorDelete(String name){
        this.name = name;
    }

    public OperatorDelete(String aAliasName, String aName, String errorInfo){
        this.aliasName = aAliasName;
        this.name = aName;
        this.errorInfo = errorInfo;
    }

    /**
     * 语法：DELETE (目标表单,过滤条件)
     * 说明：删除目标表单中符合条件的数据。目标表单可为系统中的任意表单（含子表），请谨慎执行此函数
     * 例如：删除【入库】表单的已入库信息时，同时删除【入库流水】表单中的入库流水。
     * @param list
     * @return
     * @throws Exception
     */
    @Override
    public Object executeInner(Object[] list) throws Exception {
        if (Objects.isNull(list) && list.length < Constant.LENGTH) {
            return null;
        }
        // 目标表单
        Object targetForm = list[0];
        // 过滤条件
        String condition = String.valueOf(list[1]);
        condition = condition.replaceAll(MathEnum.AND.getFormula(), MathEnum.JOIN.getFormula());
        ExpressRunner runner = new ExpressRunner();
        DefaultContext<String, Object> context = new DefaultContext<String, Object>();
        Object result = runner.execute(condition, context, null, true, false);
        Map<String, Object> returnResult = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        returnResult.put("targetForm", targetForm);
        returnResult.put("condition", result);
        return returnResult;
    }
}
